//
//  WBHomeViewController.swift
//  练手微博
//
//  Created by yuency yang on 27/06/2017.
//  Copyright © 2017 yuency yang. All rights reserved.
//

import UIKit

//定义全局常量, 尽量使用 private 修饰, 否则到处都可以访问
private let cellID = "cellID"

class WBHomeViewController: WBBaseViewController {
    
    //视图模型 懒加载
    lazy var listViewModel = WBStatusListViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    /// 加载数据
    override func loadData() {
        
        listViewModel.loadStatus(pullUp: isPullUp) { (isSuccess, shouldRefresh) in
            
            //结束刷新
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标记
            self.isPullUp = false
            //刷新表格
            if shouldRefresh {
                self.tableView?.reloadData()
            }
        }
    }
    
    /// 显示好友
    func showFriends() -> () {
        let vc = WBDemoViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
}


// MARK: - 表格数据源方法
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //1.取 cell (注册 cell 的 dequeue 要写成带 indexpath 的)
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        //2.设置 cell
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        //3.返回 cell
        return cell
    }
    
    
}


// MARK: - 设置界面
extension WBHomeViewController {
    
    override func setupTableView() {
        super.setupTableView()
        
        //使用扩展里面自己写的按钮r
        //这里要使用自己的 NavItem, 系统的是 navigationItem.leftBarButtonItem = ....
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target:  self, action: #selector(showFriends))
        
        //注册 cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        
        
        setupNaviTitle()
    }
    
    
    /// 设置导航栏标题
    func setupNaviTitle() {
        let title = WBNetworkManager.shared.userAccount.screen_name
        let button = WBTitleButton(title: title)
        navItem.titleView = button
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
 
    
    @objc func clickTitleButton(btn: UIButton) {
        
        //设置选中状态
        btn.isSelected = !btn.isSelected
    }
    
}



//设置导航栏按钮
//这种方式无法高亮显示按钮
//navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))


//swift 调用 OC 返回的 instancetype 的方法判断不出是否可选 , 所以显示写出类型
/*
 let btn: UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
 btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
 navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
 */


